
function BizProcCoordinator(pageId, baseUrl)
{
	this.pageId = pageId;
	this.baseUrl = baseUrl + '?';
	this.bizProcs = new Array();
}

BizProcCoordinator.prototype.addBizProc = function(bizProc)
{
	this.bizProcs.push(bizProc);
}

BizProcCoordinator.prototype.triggerEvent = function(bizProc, eventInfo)
{
	var doc = nitobi.xml.createXmlDoc('<ClientEvent />');
	var docEl = doc.documentElement;
	docEl.setAttribute('bizProcId', bizProc==null?'':bizProc.id);
	eventInfo.addState(docEl);
	for(var p = 0; p < this.bizProcs.length; p++)
		this.processBizProc(doc, docEl, this.bizProcs[p]);
	
	return this.sendRequest(doc);
}

BizProcCoordinator.prototype.processBizProc = function(doc, docEl, bizProc)
{
	var bizProcEl = doc.createElement('BizProc');
	bizProc.getState(bizProcEl);
	docEl.appendChild(bizProcEl);
}

BizProcCoordinator.prototype.sendRequest = function(doc)
{
	var xhr = new nitobi.ajax.HttpRequest();
	xhr.handler = this.baseUrl + 'pi=' + this.pageId + '&RequestType=' + doc.documentElement.getAttribute('event');
	xhr.async = false;
	xhr.responseType = 'xml';
	var webRespDoc = xhr.post(nitobi.xml.serialize(doc.documentElement));
	
	return this.processWebResponse(webRespDoc);
}

BizProcCoordinator.prototype.processWebResponse = function(doc)
{
    if (doc.documentElement != null && doc.documentElement.getAttribute("gotoView") != null)
        window.location.href = doc.documentElement.getAttribute("gotoView");
    else
    {
        var tempBizProcs = new Array();
        var firstLen = this.bizProcs.length;
        for (var pos = 0; pos < this.bizProcs.length; pos++)
        {
            var process = true;
            for (var p2 = 0; p2 < tempBizProcs.length; p2++)
            {
                if (tempBizProcs[p2] == this.bizProcs[pos])
                {
                    process = false;
                    break;
                }
            }
            if (process)
            {
                tempBizProcs.push(this.bizProcs[pos]);
                this.bizProcs[pos].processControlStates(doc);
                if (firstLen != this.bizProcs.length)
                {
                    pos = 0;
                    firstLen = this.bizProcs.length;
                }
            }
        }
        return (doc.documentElement == null) ? "" : doc.documentElement.getAttribute("curId");
    }
}

function EventInfo(ctlId, eventId, val)
{
	this.ctlId = ctlId;
	this.eventId = eventId;
	this.val = val;
}

EventInfo.prototype.addState = function(eventEl)
{
	eventEl.setAttribute('event', this.eventId);
	eventEl.setAttribute('id', this.ctlId);
	if(this.val != null)
	    eventEl.setAttribute('value', this.val);	
}
function ClientBizProc(id)
{
	this.id = id;
	this.ClientControls = new Array();
	bizProcManager.addBizProc(this);
}

ClientBizProc.prototype.addClientControl = function(ctl)
{
	for(var pos = 0; pos < this.ClientControls.length;pos++)
		if(this.ClientControls[pos].id == ctl.id)
		{
			this.ClientControls[pos] = ctl;
			return;
		}
	this.ClientControls[this.ClientControls.length] = ctl;
}

ClientBizProc.prototype.triggerEvent = function(ctlId, eventId, val)
{
	bizProcManager.triggerEvent(this, new EventInfo(ctlId, eventId, val));
}

ClientBizProc.prototype.getState = function(bizProcEl)
{
	bizProcEl.setAttribute('id', this.id);
	this.insertControlStates(bizProcEl.ownerDocument, bizProcEl);
}

ClientBizProc.prototype.processControlStates = function(doc)
{
	for(var pos = 0; pos < this.ClientControls.length; pos++)
	{
		var clientCtl = this.ClientControls[pos];
		var clientId = clientCtl.id;
		if(clientCtl.dhtmlObj != null)
		{
			if(clientId == null || clientId == '')
				clientId = clientCtl.dhtmlObj.id;
			var stateEl = doc.selectSingleNode("//ControlStates/State[@id = '" + clientId + "']");
			if(stateEl != null && clientCtl.setState != null)
				clientCtl.setState(stateEl);
		}
	}	
}

ClientBizProc.prototype.insertControlStates = function(doc, bizProcEl)
{
	var states = doc.createElement('ControlStates');
	bizProcEl.appendChild(states);
	for(var pos = 0; pos < this.ClientControls.length; pos++)
	{
		var clientCtl = this.ClientControls[pos];
		if(clientCtl.getState != null && clientCtl.dhtmlObj != null)
		{
			var stateEl = doc.createElement('State');
			var clientId = clientCtl.id;
			if(clientId == null || clientId == '')
				clientId = clientCtl.dhtmlObj.id;
			stateEl.setAttribute('id', clientId);
			this.ClientControls[pos].getState(stateEl);
			states.appendChild(stateEl);
		}
	}
}

function ClientControl(dhtmlObj, setStateFun, getStateFun, showBizObjFun, updateBizObjFun)
{
	this.dhtmlObj = dhtmlObj;
	this.setState = setStateFun;
	this.getState = getStateFun;
	this.showBizObj = showBizObjFun;
	this.updateBizObj = updateBizObjFun;
	if(dhtmlObj != null)
	    this.id = dhtmlObj.id;
}


function DivResizer(div){
	this.div=div;
	this.box=document.getElementById("ebagridresizebox_");
	if(this.box==null){
	this.box=document.createElement("div");
	this.box.id="ebagridresizebox_";
	document.body.appendChild(this.box);
	this.box.className="ntbcolumnresizeline";
	}
}

DivResizer.prototype.startResize=function(div,evt){
this.div=div;
var _2cb=null;
var x,y;
var _2ce=nitobi.html.getEventCoords(evt);
x=_2ce.x;
y=_2ce.y;
this.x=x;
this.y=y;
var w=div.pixelWidth;
var h=div.pixelHeight;
var L=div.offsetLeft;
var T=div.offsetTop;
this.resizeW=(Math.abs((x-L)-w)<3)||((Math.abs((y-T)-h)<16)&&(Math.abs((x-L)-w)<16));
this.resizeH=(Math.abs((y-T)-h)<3)||((Math.abs((y-T)-h)<16)&&(Math.abs((x-L)-w)<16));
if(this.resizeW||this.resizeH){
this.box.style.cursor=(this.resizeW&&this.resizeH)?"NW-Resize":(this.resizeW)?"W-Resize":"N-Resize";
this.box.style.visibility="visible";
this.box.style.width=(x-L)+"px";
this.box.style.height=(y-T)+"px";
var _2d3=nitobi.drawing.align.SAMEWIDTH|nitobi.drawing.align.SAMEHEIGHT|nitobi.drawing.align.ALIGNTOP|nitobi.drawing.align.ALIGNLEFT;
nitobi.drawing.align(this.box,this.div,_2d3,0,0,0,0,false);
this.dd=new nitobi.ui.DragDrop(this.box,false,false);
this.dd.onDragStop.subscribe(this.endResize,this);
this.dd.onMouseMove.subscribe(this.resize,this);
this.dd.startDrag(evt);
}
};

DivResizer.prototype.resize=function(){
var x=this.dd.x;
var y=this.dd.y;
var L=this.div.offsetLeft;
var T=this.div.offsetTop;
this.box.style.visibility="visible";
if(this.resizeW){
this.box.style.width=(x-L)+"px";
}
if(this.resizeH){
this.box.style.height=(y-T)+"px";
}
};

DivResizer.prototype.endResize=function(){
var x=this.dd.x;
var y=this.dd.y;
this.box.style.visibility="hidden";
var _2da=this.div.pixelWidth;
var _2db=this.div.pixelHeight;
var _2dc=parseInt(_2da)+((this.resizeW)?x-this.x:0);
var _2dd=parseInt(_2db)+((this.resizeH)?y-this.y:0);
if(isNaN(_2dc)||isNaN(_2dd)){
return;
}
if(_2dc>20&&_2dd>20){
this.div.style.width = _2dc + "px";
this.div.style.height = _2dd + "px";
}
};

DivResizer.prototype.dispose=function(){
this.grid=null;
};

function parseUsaDate(date){
debugger;
	return new Date(date.replace(/^(\d+).(\d+).(\d+).(.*)$/,"$3/$1/$2 $4"));
};

function toUsaDate(date){
	if(nitobi.base.DateMath.invalid(date)){
		return "";
	}
	var pz=nitobi.lang.padZeros;
	return pz(date.getMonth()+1)+"-"+pz(date.getDate())+"-"+date.getFullYear();
};


//-----------

function setComboSelectedRowByKey(combo, key)
{
//debugger;
	var ds =combo.GetList().GetXmlDataSource();	
	var numRows = ds.GetNumberRows();
	for(var p = 0; p < numRows; p++)
		if(ds.GetRow(p)[0] == key)
		{
			window.document.getElementById(combo.GetId()+'SelectedRowIndex').value = p;
			combo.GetList().SetSelectedRow(p);
			combo.SetTextValue(ds.GetRow(p)[1]);
			break;
		}
}
function getComboSelectedRowKey(combo)
{
    //debugger;
    var fld = $(combo.GetId() + 'SelectedValue0');
    return fld.value;
//	var ds =combo.GetList().GetXmlDataSource();	
//	var index = combo.GetList().GetSelectedRowIndex();
//	if(index == -1)
//		index = window.document.getElementById(combo.GetId()+'SelectedRowIndex').value;
//	var numRows = ds.GetNumberRows();
//	return (index == null || index < 0 || numRows == 0)?null:ds.GetRow(index)[0];
}

function setComboState(stateEl)
{
	var refresh = stateEl.getAttribute('refresh');
	if(refresh != null && this.dhtmlObj.GetMode() != "unbound")
	{
	    var cl = this.dhtmlObj.GetList();
        cl.Clear();
	    cl._syx = this;
	    cl.GetPage(0, 50, null,0, null, function(EBAComboSearchNewRecords,list){
		    //setSelectedRowByKey(list._syx.dhtmlObj,list._syx.lastKey);
	    });
	}
}

function getComboState(stateEl)
{
//	var lastKey = getSelectedRowKey(this.dhtmlObj);
//	if((lastKey == null || lastKey.length == 0) && this.lastKey != null)
//		lastKey = this.lastKey;
//	//stateEl.setAttribute('selected', lastKey);
//	stateEl.setAttribute('selected', this.dhtmlObj.GetList().GetSelectedRowIndex());
}


//-----------------
function setInnerHtmlWithScript(element, html) {
document.getElementById(element).innerHTML=html;

var re = /<script\b[\s\S]*?>([\s\S]*?)<\//ig;
var match;
while (match = re.exec(html)) {
eval(match[1]);
}
};
